package com.campuscarbooking.api.controller;

import com.campuscarbooking.api.dto.BusScheduleDTO;
import com.campuscarbooking.api.dto.PageResponse;
import com.campuscarbooking.api.dto.request.ScheduleRequest;
import com.campuscarbooking.api.dto.response.ScheduleResponse;
import com.campuscarbooking.api.model.BusSchedule;
import com.campuscarbooking.api.service.BusScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "班车管理接口")
@RestController
@RequestMapping("/schedules")
@RequiredArgsConstructor
public class ScheduleController {

    private final BusScheduleService busScheduleService;

    @ApiOperation(value = "获取班车列表", notes = "根据路线方向和类型获取班车列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "routeDirection", value = "路线方向 (A_to_B/B_to_A)", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "isFixed", value = "是否固定班车", required = false, dataTypeClass = Boolean.class),
        @ApiImplicitParam(name = "page", value = "页码", required = false, dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "size", value = "每页大小", required = false, dataTypeClass = Integer.class)
    })
    @GetMapping
    public ResponseEntity<PageResponse<BusScheduleDTO>> getSchedules(
            @RequestParam BusSchedule.RouteDirection routeDirection,
            @RequestParam(defaultValue = "true") boolean isFixed,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ResponseEntity.ok(busScheduleService.getSchedules(routeDirection, isFixed, page, size));
    }

    @ApiOperation(value = "根据ID获取班车", notes = "根据ID获取班车详情")
    @GetMapping("/{id}")
    public ResponseEntity<BusScheduleDTO> getScheduleById(
            @ApiParam(value = "班车ID", required = true) @PathVariable Long id) {
        return ResponseEntity.ok(busScheduleService.getScheduleById(id));
    }

    @ApiOperation(value = "创建班车", notes = "创建新的班车（仅管理员）")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<BusScheduleDTO> createSchedule(
            @ApiParam(value = "班车信息", required = true) @Valid @RequestBody BusScheduleDTO busScheduleDTO) {
        return ResponseEntity.ok(busScheduleService.createSchedule(busScheduleDTO));
    }

    @ApiOperation(value = "更新班车", notes = "更新现有班车信息（仅管理员）")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<BusScheduleDTO> updateSchedule(
            @ApiParam(value = "班车ID", required = true) @PathVariable Long id,
            @ApiParam(value = "班车信息", required = true) @Valid @RequestBody BusScheduleDTO busScheduleDTO) {
        return ResponseEntity.ok(busScheduleService.updateSchedule(id, busScheduleDTO));
    }

    @ApiOperation(value = "删除班车", notes = "删除班车（仅管理员）")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Void> deleteSchedule(
            @ApiParam(value = "班车ID", required = true) @PathVariable Long id) {
        busScheduleService.deleteSchedule(id);
        return ResponseEntity.noContent().build();
    }
} 